﻿using MDK.Common;
using MDK.Model.Models;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Dynamic;

namespace MDK.Common
{
    public class IDatatoFlow
    {

        /// <summary>
        /// 工位
        /// </summary>
        /// <param name="info">挑选记录</param>
        public static List<T_Position> GetPosition()
        {
            return WebAPIHelper.Get<List<T_Position>>("Position/FindList?jsonWhere=&orderPropertyName=&isAsc=false");
        }
        /// <summary>
        /// 获取离心机配置
        /// </summary>
        /// <returns></returns>
        public static T_CentrifugeSetting GetCentrifugeSetting()
        {
            return WebAPIHelper.Get<T_CentrifugeSetting>("CentrifugeSetting/Find?jsonWhere=&orderPropertyName=&isAsc=false");
        }
        /// <summary>
        /// 更改离心机是否启用
        /// </summary>
        /// <param name="SysKey"></param>
        /// <param name="IsEnable">是否启用</param>
        /// <returns></returns>
        public static void UpdateCentrifugeState(string SysKey,bool IsEnable)
        {
            PropertyHelper jsonWhere = new PropertyHelper();
            jsonWhere.Addproperty(CompareType.Equal, "SysKey", SysKey);
            string url = string.Format("Systemsetting/Find?jsonWhere={0}", JsonConvert.SerializeObject(jsonWhere.propertyList));
            var mod = WebAPIHelper.Get<T_Systemsetting>(url);
            mod.Is_Enable = IsEnable;
            WebAPIHelper.Post<T_Systemsetting>("Systemsetting/Modify", mod);
        }
        /// <summary>
        /// 获取离心机是否启用
        /// </summary>
        /// <param name="SysKey"></param>
        /// <returns></returns>
        public static bool IsCentrifugeEnable(string SysKey)
        {
            PropertyHelper jsonWhere = new PropertyHelper();
            jsonWhere.Addproperty(CompareType.Equal, "SysKey", SysKey);
            string url = string.Format("Systemsetting/Find?jsonWhere={0}", JsonConvert.SerializeObject(jsonWhere.propertyList));
            var mod=WebAPIHelper.Get<T_Systemsetting>(url);
            return mod == null ? false : mod.Is_Enable;
        }
        /// <summary>
        /// 获取所有试管未删除的试管
        /// </summary>
        public static List<T_Sample> GetSamples()
        {
            PropertyHelper jsonWhere = new PropertyHelper();
            jsonWhere.Addproperty(CompareType.Equal, "Is_Delete", false);
            string url = string.Format("Sample/FindList?jsonWhere={0}&orderPropertyName=&isAsc=false", JsonConvert.SerializeObject(jsonWhere.propertyList));
            return WebAPIHelper.Get<List<T_Sample>>(url);
        }

        /// <summary>
        /// 插入试管
        /// </summary>
        /// <param name="sample"></param>
        public static void SampleInsert(T_Sample sample)
        {
            if(!string.IsNullOrWhiteSpace(sample.Id))
            WebAPIHelper.Post<T_Sample>("Sample/Add", sample);
        }

        /// <summary>
        /// 更新试管信息
        /// </summary>
        /// <param name="sample"></param>
        public static void SampleUpdate(T_Sample sample)
        {
            PropertyHelper jsonWhere = new PropertyHelper();
            jsonWhere.Addproperty(CompareType.Equal, "Id", sample.Id);
            string url = string.Format("Sample/Find?jsonWhere={0}", JsonConvert.SerializeObject(jsonWhere.propertyList));
            var model = WebAPIHelper.Get<T_Sample>(url);
            if (model != null)
            {
                model.PostionId = sample.PostionId;
                model.Index = sample.Index;
                model.ReBarCode = sample.ReBarCode;
                model.State = sample.State;
                model.Is_Delete = sample.Is_Delete;
                WebAPIHelper.Post<T_Sample>("Sample/Modify", model);
            }
        }

        public static void SampleDelete(T_Sample sample)
        {
            PropertyHelper jsonWhere = new PropertyHelper();
            jsonWhere.Addproperty(CompareType.Equal, "Id", sample.Id);
            string url = string.Format("Sample/Find?jsonWhere={0}", JsonConvert.SerializeObject(jsonWhere.propertyList));
            var model = WebAPIHelper.Get<T_Sample>(url);
            if (model != null)
            {
                model.Is_Delete = sample.Is_Delete;
                WebAPIHelper.Post<T_Sample>("Sample/Modify", model);
            }
        }

        /// <summary>
        /// 更新试管信息
        /// </summary>
        /// <param name="sample"></param>
        public static void FreezerSampleUpdate(string Id, int target, int Index)
        {
            PropertyHelper jsonWhere = new PropertyHelper();
            jsonWhere.Addproperty(CompareType.Equal, "Id", Id);
            string url = string.Format("Sample/Find?jsonWhere={0}", JsonConvert.SerializeObject(jsonWhere.propertyList));
            var model = WebAPIHelper.Get<T_Sample>(url);
            if (model != null)
            {
                model.PostionId = "10" + (target + 1) + "0" + (Index + 1);
                WebAPIHelper.Post<T_Sample>("Sample/Modify", model);
            }
        }

        /// <summary>
        /// 试管历史
        /// </summary>
        /// <param name="info"></param>
        public static void InsertSampleHistory(T_HistorySample info)
        {
            WebAPIHelper.Post<T_HistorySample>("HistorySample/Add", info);
        }

        /// <summary>
        /// 离心记录插入数据库更新试管CentrifugeId
        /// </summary>
        /// <param name="info">离心模型</param>
        /// <param name="SampleIds">试管ID</param>
        public static void CentrifugeInsert(T_Centrifuge info, string[] SampleIds)
        {
            dynamic obj = new ExpandoObject();
            obj.Centrifuge = info;
            obj.SampleIds = SampleIds;
            WebAPIHelper.Post<dynamic>("Centrifuge/CentrifugeInsert", JsonConvert.SerializeObject(obj));
        }

        /// <summary>
        /// 离心完成更新状态
        /// </summary>
        /// <param name="info"></param>
        /// <param name="SampleIds"></param>
        public static void CentrifugeCompleted(string centrifugeId)
        {
            dynamic obj = new ExpandoObject();
            obj.CentrifugeId = centrifugeId;
            WebAPIHelper.Post<dynamic>("Centrifuge/CentrifugeCompleted", JsonConvert.SerializeObject(obj));
        }


        /// <summary>
        /// 挑选记录
        /// </summary>
        /// <param name="info">挑选记录</param>
        public static void InsertPick(T_Pick info)
        {
            WebAPIHelper.Post<T_Pick>("Pick/Add", info);
        }


        /// <summary>
        /// 冷藏温度
        /// </summary>
        /// <param name="info">温度</param>
        public static void InsertTemperatures(T_Temperatures info)
        {
            WebAPIHelper.Post<T_Temperatures>("Temperatures/Add", info);
        }

        /// <summary>
        /// 任务更新
        /// </summary>
        /// <param name="task"></param>
        public static void TaskUpdate(T_Task task)
        {
            PropertyHelper jsonWhere = new PropertyHelper();
            jsonWhere.Addproperty(CompareType.Equal, "Id", task.Id);
            string url = string.Format("Task/Find?jsonWhere={0}", JsonConvert.SerializeObject(jsonWhere.propertyList));
            var model = WebAPIHelper.Get<T_Task>(url);
            if (model != null)
            {
                model.State = task.State;
                WebAPIHelper.Post<T_Task>("Task/Modify", model);
            }
        }
       
    }
}
